package com.xsl.culture.mybasevideoview.utils;

import com.xsl.culture.mybasevideoview.MainActivity;
import com.xsl.culture.mybasevideoview.controller.NetworkReq;
import com.xsl.culture.mybasevideoview.model.ChapterListInfo;
import com.xsl.culture.mybasevideoview.view.LanguageActivity;

import java.util.ArrayList;
import java.util.List;

public class StringUtil {
    public static int string2int(String str) {
        return string2int(str, 0);
    }

    public static int string2int(String str, int def) {
        try {
            return Integer.valueOf(str);
        } catch (Exception e) {
        }
        return def;
    }

    public static String getChapterNum(int code) {
        String str = "";
        if (MainActivity.curLangugue == LanguageActivity.english) {
            str = "Chapter " + code ;
        } else if (MainActivity.curLangugue == LanguageActivity.chinese) {
            str = "第" + int2chineseNum(code) + "章";
        } else if (MainActivity.curLangugue == LanguageActivity.cantonese) {
            str = "第" + int2chineseNum(code) + "章";
        }
        return str;
    }
    public static String getChapterAll(int code) {
        List<ChapterListInfo.DataBean> data = NetworkReq.getInstance().getChapterListInfo().getData();
        String str = "";
        if (MainActivity.curLangugue == LanguageActivity.english) {
            str = "Chapter " + code + "  " + data.get(code - 1).getEnName();
        } else if (MainActivity.curLangugue == LanguageActivity.chinese) {
            str = "第" + int2chineseNum(code) + "章   " + data.get(code - 1).getName();
        } else if (MainActivity.curLangugue == LanguageActivity.cantonese) {
            str = "第" + int2chineseNum(code) + "章   " + data.get(code - 1).getTcName();
        }
        return str;
    }

    public static String getChapterName(int code) {
        List<ChapterListInfo.DataBean> data = NetworkReq.getInstance().getChapterListInfo().getData();
        String str = "";
        if (MainActivity.curLangugue == LanguageActivity.english) {
            str = data.get(code - 1).getEnName();
        } else if (MainActivity.curLangugue == LanguageActivity.chinese) {
            str = data.get(code - 1).getName();
        } else if (MainActivity.curLangugue == LanguageActivity.cantonese) {
            str = data.get(code - 1).getTcName();
        }
        return str;
    }

    public static ArrayList<String> getChapterList() {
        List<ChapterListInfo.DataBean> data = NetworkReq.getInstance().getChapterListInfo().getData();
        ArrayList<String> datas = new ArrayList<>();
        if (null == data) {
            return datas;
        }
        for (ChapterListInfo.DataBean dataBean : data) {
            String str = "";
            int code = Integer.parseInt(dataBean.getCode());
            if (MainActivity.curLangugue == LanguageActivity.english) {
                str = dataBean.getCode() + "  " + dataBean.getEnName();
            } else if (MainActivity.curLangugue == LanguageActivity.chinese) {
                str = "第" + int2chineseNum(code) + "章   " + dataBean.getName();
            } else if (MainActivity.curLangugue == LanguageActivity.cantonese) {
                str = "第" + int2chineseNum(code) + "章   " + dataBean.getTcName();
            }
            datas.add(str);
        }
        return datas;
    }


    public static String int2chineseNum(int src) {
        final String num[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        final String unit[] = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String dst = "";
        int count = 0;
        if (10 <= src && src < 20) {
            int s = src % 10;
            return ((unit[src / 10]) + num[s]).replaceAll("零", "");
        } else {
            while (src > 0) {
                dst = (num[src % 10] + unit[count]) + dst;
                src = src / 10;
                count++;
            }
            return dst.replaceAll("零[千百十]", "零").replaceAll("零+万", "万")
                    .replaceAll("零+亿", "亿").replaceAll("亿万", "亿零")
                    .replaceAll("零+", "零").replaceAll("零$", "");
        }
    }
}
